<?php defined('SYSPATH') OR die('No direct access allowed.');

class Action_Controller extends Crud_Controller {

	protected $form_criar = array(
	        'name'			=> '',
			'type'			=> '',
			'modules'		=> '',
	    );
	
	public function __construct(){
		parent::__construct(); // This must be included
	}

	public function index(){
		url::redirect('action/listar');
	}

	public function listar($pg_num=0){
		$total_items = ORM::factory('auth_action')->count_all();
		$this->pagination = new Pagination(array(
	        'base_url'    => 'action/listar/', // Set our base URL to controller 'items' and method 'page'
	        'uri_segment' => 'listar', // Our URI will look something like http://domain/items/page/19
	        'total_items' => $total_items // Total number of items.
    	));
		$offset = $this->pagination->sql_offset;
		$itens_por_pagina = $this->pagination->items_per_page;
		
		$actions = ORM::factory('auth_action')->find_all($itens_por_pagina,$offset);
		$this->create_view('action/list');
		$this->view->actions = $actions;
		$this->view->pg_num = $pg_num;
		$this->view->pagination = $this->pagination;
		$this->view->cabecalho->title .= '::Listar';
		$this->view->render(TRUE);
	}

	public function criar($msg=""){
		$this->create_view('action/criar');
		$this->view->msg = $msg;
		$this->view->modules = ORM::factory('module')->select_list('id', 'type');
		$this->view->form = $this->form_criar;
		$this->view->cabecalho->title .= '::Criar';
		$this->view->render(TRUE);
	}

	public function salvar(){

	}

	public function editar($msg=""){
		$pg_num = $this->input->post('pg_num');
		$validation = new Validation($this->input->post());
		$validation->add_rules('action_id','required');
        if(!$validation->validate()){
        	url::redirect('action/listar/'.$pg_num);
        }
        $actions_id = $this->input->post('action_id');
		$actions = array();
		foreach ($actions_id as $id){
			$actions[] = ORM::factory('auth_action', $id); 
		}
		$this->create_view('action/editar');
		$this->view->msg = $msg;
		$this->view->actions = $actions;
		$this->view->modules = ORM::factory('module')->select_list('id', 'type');
		$this->view->pg_num = $pg_num;
		$this->view->cabecalho->title .= '::Editar';
		$this->view->render(TRUE);
	}

	public function deletar($msg=""){
		$pg_num = $this->input->post('pg_num');
		$validation = new Validation($this->input->post());
		$validation->add_rules('action_id','required');
        if(!$validation->validate()){
        	url::redirect('action/listar/'.$pg_num);
        }
        $actions_id = $this->input->post('action_id');
		$actions = array();
		foreach ($actions_id as $id){
			$actions[] = ORM::factory('auth_action', $id); 
		}
		$this->create_view('action/deletar');
		$this->view->msg = $msg;
		$this->view->actions = $actions;
		$this->view->pg_num = $pg_num;
		$this->view->cabecalho->title .= '::Deletar';
		$this->view->render(TRUE);
	}

	public function ver(){
		$pg_num = $this->input->post('pg_num');
		$validation = new Validation($this->input->post());
		$validation->add_rules('action_id','required');
        if(!$validation->validate()){
        	url::redirect('action/listar/'.$pg_num);
        }
        $actions_id = $this->input->post('action_id');
		$actions = array();
		foreach ($actions_id as $id){
			$actions[] = ORM::factory('auth_action', $id); 
		}
		$this->create_view('action/ver');
		$this->view->actions = $actions;
		$this->view->pg_num = $pg_num;
		$this->view->cabecalho->title .= '::Ver';
		$this->view->render(TRUE);
	}
	
	public function module($module_id){
		
		$actions = ORM::factory('auth_action')
						->where('module_id', $module_id)
						->find_all();
		$this->create_view('action/list');
		$this->view->actions = $actions;
		$this->view->pg_num = 0;
		$this->view->pagination = '';
		$this->view->cabecalho->title .= '::Listar';
		$this->view->render(TRUE);
	}

} // End Welcome Controller